<?php

$platform = isset($_GET['platform']) ? $_GET['platform'] : 'huya';
$roomid = isset($_GET['id']) ? $_GET['id'] : '21159589';

$api = "https://yj1211.work:8014/api/live/getRealUrl?platform=".$platform."&roomId=".$roomid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("User-Agent: okhttp/4.3.1"));
$data = curl_exec($ch);

curl_close($ch);
$obj = json_decode($data);
    if (isset($obj->data->OD)) {
    $playUrl = $obj->data->OD;
} else if (isset($obj->data->HD)) {
    $playUrl = $obj->data->HD;
} else if (isset($obj->data->SD)) {
    $playUrl = $obj->data->SD;
}

header('Location:'.$playUrl);
exit;
function huya_proxy($roomid) {

    $bstrURL = "https://mp.huya.com/cache.php?m=Live&do=profileRoom&roomid=$roomid";
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$bstrURL);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    $data = curl_exec($ch);
    curl_close($ch);
    
    $json = json_decode($data);
    $bStreamLst = $json->data->stream->baseSteamInfoList[1];
    
    $sStreamName = $bStreamLst->sStreamName;
    $sHlsUrl = $bStreamLst->sHlsUrl;
    $sAntiCode = $bStreamLst->sHlsAntiCode;
    #$sFlvUrl = $bStreamLst->sFlvUrl;
    #$sAntiCode = $bStreamLst->sFlvAntiCode;
    
    parse_str($sAntiCode,$params);
    $fm = base64_decode($params['fm']);
    $wsTime = $params['wsTime'];
    $ctype = $params['ctype'];
    $seqid = time().'000'; // uid(0) + date.now() == 13位时间戳
    $i = md5($seqid.'|'.$ctype.'|100'); // t = 100 若为动态请从AntiCode获取
    
    $wsSecret = md5(str_replace(['$0','$1','$2','$3'],['0',$sStreamName,$i,$wsTime],$fm));  // uid_streamname_hash_wstime
    
    $szURL = $sHlsUrl.'/'.$sStreamName.'.m3u8?wsSecret='.$wsSecret.'&wsTime='.$wsTime.'&uid=0&fm='.urlencode(base64_encode($fm)).'&ctype='.$ctype.'&seqid='.$seqid.'&ver=1&t=100';
    #$szURL = $sFlvUrl.'/'.$sStreamName.'.flv?wsSecret='.$wsSecret.'&wsTime='.$wsTime.'&uid=0&fm='.urlencode(base64_encode($fm)).'&ctype='.$ctype.'&seqid='.$seqid.'&ver=1&t=100';
    
    return $szURL;
}
?>
